package com.cx.nio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 *
 */
public class BioServer {
    private static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws IOException {

        // 启动服务，绑定9000端口
        ServerSocket serverSocket = new ServerSocket(9000);
        while (true) {
            Socket clientSocket = serverSocket.accept();// 阻塞
            System.out.println("建立新的链接" + clientSocket);
            executorService.execute(()-> handler(clientSocket));
//            handler(clientSocket);
        }
    }
    private static void handler(Socket clientSocket) {
        while (true){
            try {
                byte[] bytes = new byte[1024];
                int data = clientSocket.getInputStream().read(bytes); // 阻塞
                if(data !=-1){
                    System.out.println("读取数据 "+new String(bytes,0,data));
                }
            }catch (Exception e){ }
        }
    }
}
